package com.zzyl.controller.manager;

import com.zzyl.base.PageBean;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.AlertRuleDto;
import com.zzyl.dto.AlertRuleQueryDto;
import com.zzyl.service.AlertRuleService;
import com.zzyl.vo.AlertRuleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/alert-rule")
@Api(tags = "报警规则相关接口")
public class AlertRuleController {

    @Resource
    private AlertRuleService alertRuleService;

    @ApiOperation(value = "新增报警规则")
    @PostMapping("/create")
    public ResponseResult<Void> createAlertRule(@RequestBody AlertRuleDto dto){
        log.info("新增报警规则:{}",dto);
        alertRuleService.createRule(dto);
        return ResponseResult.success();
    }



    @ApiOperation(value = "获取单个告警规则")
    @GetMapping("/read/{id}")
    public ResponseResult<AlertRuleVo> readAlertRule(@PathVariable Long id ){
        log.info("获取单个告警规则:{}",id);
        AlertRuleVo alertRuleVo = alertRuleService.readAlertRule(id);
        return ResponseResult.success(alertRuleVo);
    }


    @ApiOperation(value = "修改告警规则")
    @PutMapping("/update/{id}")
    public ResponseResult<Void> updateAlertRule(@PathVariable Long id,
                                                @RequestBody AlertRuleDto dto){
        log.info("更新告警规则:{}",id);
        alertRuleService.updateAlertRule(id,dto);
        return ResponseResult.success();
    }




    @ApiOperation(value = "删除告警规则")
    @DeleteMapping("/delete/{id}")
    public ResponseResult<Void> deleteAlertRule(@PathVariable Long id){
        log.info("删除告警规则:{}",id);
        alertRuleService.deleteAlertRule(id);
        return ResponseResult.success();
    }



    @ApiOperation(value = "分页获取告警规则列表")
    @GetMapping("/get-page")
    public ResponseResult<PageBean<AlertRuleVo>> getAlertRulePage(AlertRuleQueryDto dto){
        log.info("分页获取告警规则列表:{}",dto);
        PageBean<AlertRuleVo> pageBean = alertRuleService.getAlertRulePage(dto);
        return ResponseResult.success(pageBean);
    }



    @ApiOperation("启用/禁用")
    @PutMapping("/{id}/status/{status}")
    public ResponseResult<Void> enableOrDisable(@PathVariable Long id,
                                                @PathVariable Integer status){
        log.info("启用/禁用告警规则：{},{}", id, status);
        alertRuleService.enableOrDisable(id, status);
        return ResponseResult.success();
    }
}
